﻿namespace WebCommon.Up
{
    using System.IO;
    using System.Web;

    using WebCommon;
    using WebCommon.IO;

    /// <summary>
    /// 图片上传
    /// </summary>
    public class UploadImg : UploadFile
    {
        public string ImgPath { get; private set; }


        /// <summary>
        /// 允许上传的集合
        /// </summary>
        public UploadImg(HttpPostedFileBase hb,string rootpath)
            : base(hb, rootpath)
        {
            this.TypeList = new[] { ".jpg", ".png", ".gif", ".jpeg" };
        }
        /// <summary>
        /// 设置压缩图片
        /// </summary>
        /// <param name="hb"></param>
        /// <param name="heght"></param>
        /// <param name="width"></param>
        /// <param name="flag"></param>
        public UploadImg(HttpPostedFileBase hb, string rootpath,int heght, int width, int flag)
            : this(hb,  rootpath)
        {
            this.flag = flag;
            this.heght = heght;
            this.width = width;
        }


        private int heght = 300;

        private int width = 300;

        private int flag = 100;

        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="path"></param>
        /// <param name="b">是否删除原图</param>
        /// <returns></returns>
        public bool Compress(string path, bool b = true)
        {
            path = path + "/" + this.SaFileName + this.GetExtension;
            if (!System.IO.Directory.Exists(path))
            {
                FileOperation.CreateDirectory(path);
            }
            bool picThumbnail = ReducedImage.GetPicThumbnail(this.SaPath, path, this.heght, this.width, this.flag);
            if (picThumbnail && b)
            {
                this.ImgPath = path;
                this.DeleteImg();
            }

            return picThumbnail;

        }


        /// <summary>
        /// 删除原图片
        /// </summary>
        public void DeleteImg()
        {
            File.Delete(this.SaPath);
        }
    }
}